<?php
/**
 * CommonFavorites
 *  
 * @author Administrator
 * @version 
 */
class Model_CommonFavorites extends Zend_Db_Table_Abstract
{
    /**
     * The default table name
     */
    protected $_name = 'tcl_common_favorites';
    
    
    
    public function getFavorites($uid) {
    	
        $db = $this->getAdapter();
        $where = $db->quoteInto("uid = ?", $uid);
     
        $row = $this->fetchAll($where)->toArray();
        
        return $row;
    }
    
    
    //删除收藏 
    public function setFavoritesDel($ids, $uid) {
    
    	$db = $this->getAdapter();

    	
    	$where = $db->quoteInto('id IN (?)', $ids);
    			//.$db->quoteInto("AND uid = ?", $uid);
    
    	try {
    		$row = $this->delete($where);
    	} catch (Exception $e) {
    		echo $e;
    		exit();
    	}
    	return $row;
    }
    
    public function getFavoriveById($id) {
        	 
        $db = $this->getAdapter();
        $where = $db->quoteInto("fav_id = ?", $id);
         
        $row = $this->fetchRow($where);

        return $row;
    }
    
    
    //添加收藏  (S)
    public function setFavorite($ids, $uid) {
        
        $sale = new Model_CarSale();
        $id_arr = explode(',', $ids);


        
        for ($i = 0; $i < count($id_arr); $i++) {
            
            
            
            $id = $id_arr[$i];
            

            $is_id = $this->getFavoriveById($id);

            if (!$is_id){
                $sale_arr = $sale->getViewName($id);
                $title = $sale_arr[0][title];
                
                $db = $this->getAdapter();
                
                $data = array(
                		'uid'         => $uid,
                		'title'       => $title,
                		'fav_id'      => $id,
                		'created_at'  => time()
                );
                
                try {
                	$row = $this->insert($data);
                } catch (Exception $e) {
                	echo $e;
                	exit();
                }
                
                //单个收藏通知用户
                    $msg = new Model_CommonMsg();
                    $msg->setMsgTo($sale_arr[0][uid], "您发布的信息被人收藏了", "您发布的编号为 <a href='/buy/view/cid/$id' target='_blank'>$title</a> 的信息被人收藏了！发布信息时上传6张以上车源图片的信息更容易快速被出售。立即去完善您的车源信息吧！");

                
            }else{
                return "1";
            }

        }

        return $row;
    }
}
